package com.kinon.gfcanteen_v1.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.kinon.gfcanteen_v1.R;
import com.kinon.gfcanteen_v1.bean.BaseBean;
import com.kinon.gfcanteen_v1.bean.PayBean;
import com.kinon.gfcanteen_v1.network.CommonSubscriber;
import com.kinon.gfcanteen_v1.network.HttpManage;
import com.kinon.gfcanteen_v1.network.StrCallback;
import com.kinon.gfcanteen_v1.util.RxUtil;
import com.kinon.gfcanteen_v1.util.SPManager;
import com.kinon.gfcanteen_v1.view.base.BaseActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import io.reactivex.disposables.Disposable;
import retrofit2.Call;
import retrofit2.Response;

/**
 * Created by lhqq on 2018-01-09.
 */

public class PayActivity extends BaseActivity implements View.OnClickListener{
    private TextView tv_title;
    private ImageView img_back;
    private TextView tv_payPrice;
    private Button btn_scan;
    private String mOrderid="";
    private String nonce_str="";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pay_layout);
        initView();
    }

    private void initView() {
        tv_title= (TextView) findViewById(R.id.tv_title);
        img_back= (ImageView) findViewById(R.id.img_back);
        tv_payPrice= (TextView) findViewById(R.id.tv_payPrice);
        btn_scan= (Button) findViewById(R.id.btn_scan);
        img_back.setVisibility(View.VISIBLE);
        tv_title.setText("支付");
        mOrderid=getIntent().getStringExtra("orderid");
        nonce_str=getIntent().getStringExtra("nonce_str");
        String strTotal=getIntent().getStringExtra("total");
        tv_payPrice.setText(strTotal);
        addListener();
    }

    private void addListener(){
        img_back.setOnClickListener(this);
        btn_scan.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.img_back:
                finish();
                break;
            case R.id.btn_scan:
                Intent intent=new Intent("com.summi.scan");
                intent.setPackage("com.sunmi.sunmiqrcodescanner");
                startActivityForResult(intent,3);
                break;
        }
    }
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==3 && data!=null){
            Bundle bundle=data.getExtras();
            ArrayList<HashMap<String, String>> result =
                    (ArrayList<HashMap<String, String>>) bundle.getSerializable("data");
            Iterator<HashMap<String, String>> it = result.iterator();
            String cardNo="";
            while (it.hasNext()){
                HashMap<String, String> hashMap = it.next();
                cardNo=hashMap.get("VALUE");
            }
            otherPay(cardNo);
        }

    }

    private void otherPay(String card){
        Call<String> call=HttpManage.getInstance().otherPay(
                SPManager.getInstance().getToken(),
                mOrderid,card,nonce_str);
        call.enqueue(new StrCallback<String>() {
            @Override
            public void onSuccess(Response<String> response, String data) {
                mToast("支付成功");
                finish();
            }

            @Override
            public void onError(int code, String error) {
                mToast(error);
            }
        });
    }
}
